package com.feisi.dao;

import com.feisi.dao.sqlProvider.LinkmanSqlProvider;
import com.feisi.domain.Linkman;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface LinkmanDao {

    /**
     * 查询所有联系人,包括客户(从customer表中获取)
     * @param page
     * @param limit
     * @return
     */
    @Select("select * from linkman limit #{page},#{limit}")
    @Results(id = "linkmanMap", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "li_name", property = "li_name"),
            @Result(column = "li_phone", property = "li_phone"),
            @Result(column = "li_e_mail", property = "li_e_mail"),
            @Result(column = "li_job", property = "li_job"),
            @Result(column = "li_sex", property = "li_sex"),
            @Result(column = "li_policymaker", property = "li_policymaker"),
            @Result(column = "li_principal", property = "li_principal"),
            @Result(column = "li_comment", property = "li_comment"),
            @Result(column = "li_create_time", property = "li_create_time"),
            @Result(column = "li_creator", property = "li_creator"),
            @Result(column = "li_customer_id", property = "customer",
                    one = @One(select = "com.feisi.dao.CustomerDao.findCustomerById", fetchType = FetchType.EAGER)),})
    List<Linkman> findAll(@Param("page") Integer page, @Param("limit") Integer limit);


    /**
     * 查询所有联系人
     *
     * @param page
     * @param limit
     * @return
     */
    /*@Select("select * from linkman limit #{page},#{limit}")
    List<Linkman> findAll(@Param("page") Integer page, @Param("limit") Integer limit);*/


    /**
     * 查询总条数
     *
     * @return
     */
    @Select("select count(*) from linkman")
    Integer linkmansTotal();


    /**
     * 根据联系人姓名查询联系人
     *
     * @param li_name
     * @return
     */
    @Select("select * from linkman where li_name=#{li_name} limit #{page},#{limit}")
    @ResultMap("linkmanMap")
    List<Linkman> findByName(@Param("li_name") String li_name, @Param("page") Integer page, @Param("limit") Integer limit);

    /**
     * 根据联系人名称查询总条数
     *
     * @return
     */
    @Select("select count(*) from linkman where li_name=#{li_name}")
    Integer linkmansTotalByName(@Param("li_name") String li_name);

    /**
     * 根据客户名查询此客户的联系人
     *
     * @param customername
     * @return
     */
    @Select("select l.* from linkman l ,customer c where c.cu_name = #{customername} limit #{page},#{limit}")
    List<Linkman> findByCustomerName(@Param("customername") String customername, @Param("page") Integer page, @Param("limit") Integer limit);


    /**
     * 根据客户名查询此客户的联系人的总条数
     *
     * @param customername
     * @return
     */
    @Select("select count(*) from linkman l ,customer c where c.cu_name = #{customername}")
    Integer linkmanTotalByCustomerName(@Param("customername") String customername);


    /**
     * 新增联系人
     *
     * @param linkman
     */
    @Insert("insert into linkman values(#{id},#{li_name},#{li_phone},#{li_e_mail},#{li_job}," +
            "#{li_sex},#{li_policymaker},#{li_principal},#{li_comment},#{li_create_time},#{li_creator},#{li_customer_id})")
    void saveLinkman(Linkman linkman);

    /**
     * 根据id删除联系人
     * 批量删除
     *
     * @param ids
     */
    @DeleteProvider(type = LinkmanSqlProvider.class, method = "deleteByIds")
    void deleteLinkman(@Param("ids") Integer[] ids);

    /**
     * 修改联系人基本信息
     * @param linkman
     */
    @Update("update linkman set li_name=#{li_name},li_phone=#{li_phone},li_e_mail=#{li_e_mail}," +
            "li_job=#{li_job},li_sex=#{li_sex},li_policymaker=#{li_policymaker},li_comment=#{li_comment} where id=#{id}")
    void updateLinkman(Linkman linkman);

}
